<template>
  <div style="width: 60%; margin: 10px auto">
    <div class="card" style="padding: 20px; display: flex; grid-gap: 20px; margin-bottom: 20px">
      <img :src="data.pet.image" alt="" style="width: 400px; height: 350px">
      <div style="flex: 1; display: flex; flex-direction: column; justify-content: space-around">
        <div style="font-size: 20px; font-weight: bold; margin-bottom: 10px">{{ data.pet.name }}</div>
        <div style="margin-bottom: 10px; display: flex; align-items: center;">
          <span style="color: #666">宠物店：</span>
          <div style="display: flex; align-items: center; cursor: pointer;" @click="router.push('/front/petShopDetail?id=' + data.pet.shopId)">
            <img style="width: 20px; height: 20px; border-radius: 50%" :src="data.shopAvatar" alt="">
            <span>{{ data.shopName }}</span>
          </div>
        </div>
        <div style="margin-bottom: 10px">
          <span style="color: #666">宠物类型：</span>
          <span>{{ data.pet.typeName }}</span>
        </div>
        <div style="margin-bottom: 10px">
          <span style="color: #666">宠物状态：</span>
          <el-tag type="success">售卖中</el-tag>
        </div>
        <div style="margin-bottom: 10px">
          <span style="color: #666">宠物性别：</span>
          <span>{{ data.pet.sex }}</span>
        </div>
        <div style="margin-bottom: 10px">
          <span style="color: #666">宠物价格：</span>
          <span style="color: red; font-size: 20px">￥{{ data.pet.price }}</span>
        </div>
        <div style="margin-bottom: 10px">
          <span style="color: #666">宠物库存：</span>
          <span style="color: orange; font-size: 20px">{{ data.pet.store }}</span>
        </div>
        <div style="text-align: justify; line-height: 20px; margin-bottom: 10px" class="line">
          <span style="color: #666">宠物简介：</span>
          <el-tooltip effect="dark myTooltip" :content="data.pet.introduce" placement="top" v-if="data.pet.introduce?.length >= 70">
            <span>{{ data.pet.introduce }}</span>
          </el-tooltip>
          <span v-else>{{ data.pet.introduce }}</span>
        </div>

        <div style="display: flex; grid-gap: 20px">
          <el-button type="danger" @click="handlePay" :disabled=" data.pet.store < 1 ">立即购买</el-button>
        </div>
      </div>
    </div>

    <div class="card" style="padding: 20px">
      <div style="font-size: 24px; font-weight: bold; padding-bottom: 10px; border-bottom: 1px solid #ddd">宠物详情</div>
      <div style="padding: 20px 0" v-html="data.pet.content" v-if="data.pet.content"></div>
      <div style="padding-top: 50px; padding-bottom: 30px; text-align: center; color: red" v-else>
        暂无宠物详情...
      </div>
    </div>

    <el-dialog title="确认收货人信息" v-model="data.formVisible" width="45%" destroy-on-close>
      <el-form ref="formRef" :rules="data.rules" :model="data.form" label-width="90px" style="padding: 20px">
        <el-form-item prop="addressId" label="收货地址">
          <el-select style="width: 100%" v-model="data.form.addressId">
            <el-option v-for="address in data.addressList" :key="address.id" :value="address.id" :label="address.name + ' - ' + address.address + ' - ' + address.phone"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.formVisible = false">取 消</el-button>
          <el-button type="primary" @click="createOrder">确 定</el-button>
        </span>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
  import {reactive, ref} from "vue";
  import router from "@/router/index.js";
  import request from "@/utils/request.js";
  import {ElMessage} from "element-plus";

  const formRef = ref()

  const data = reactive({
    id: router.currentRoute.value.query.id,
    user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
    pet: {},
    shopAvatar: null,
    shopName: null,
    formVisible: false,
    form: {},
    addressList: [],
    rules: {
      addressId: [
        {required: true, message: '请选择收货人信息', trigger: 'blur'}
      ]
    }
  })

  const load = () => {
    request.get('/pet/query?id=' + data.id).then(res => {
      if (res.code === '200') {
        data.pet = res.data
        request.get('/petShop/query?id=' + data.pet.shopId).then(res => {
          if (res.code === '200') {
            data.shopAvatar = res.data.avatar
            data.shopName = res.data.name
          } else {
            ElMessage.error(res.msg)
          }
        })
      } else {
        ElMessage.error(res.msg)
      }
    })
  }
  load()

  request.get('/address/query/all', {
    params: {
      userId: data.user.id
    }
  }).then(res => {
    if (res.code === '200') {
      data.addressList = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })

  const handlePay = () => {
    data.form = {
      petId: data.id
    }
    data.formVisible = true
  }

  // 创建宠物订单
  const createOrder = () => {
    formRef.value.validate(valid => {
      if (valid) {
        request.post('/petOrders/add', {
          petId: data.id,
          shopId: data.pet.shopId,
          addressId: data.form.addressId
        }).then(res => {
          if (res.code === '200') {
            data.formVisible = false
            ElMessage.success('下单成功')
            load()
          } else {
            ElMessage.error(res.msg)
          }
        })
      }
    })
  }

</script>

<style>
 .myTooltip {
   width: 30%;
 }
</style>